﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Collections;

namespace PasswordForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Guid newGuid;
        private void button1_Click(object sender, EventArgs e)
        {
            newGuid = Guid.NewGuid();
            txtEncode.Text = newGuid.ToString();
        }

        private void btnPassword_Click(object sender, EventArgs e)
        {
            byte[] pwdBytes = Encoding.UTF8.GetBytes(txtPassword.Text);
            byte[] encodeBytes = newGuid.ToByteArray();
            byte[] encodeBytes1 = Encoding.UTF8.GetBytes("zhang3");

            int length = Math.Max(Math.Max(pwdBytes.Length, encodeBytes.Length), encodeBytes1.Length);

            byte[] result = new byte[length];

            for (int i = 0; i < length; i++)
            {

                byte x = 0xff;
                if (i < pwdBytes.Length)
                {
                    x = pwdBytes[i];
                }

                byte y = 0xff;
                if (i < encodeBytes.Length)
                {
                    y = encodeBytes[i];
                }

                byte z = 0xff;
                if (i < encodeBytes1.Length)
                {
                    z = encodeBytes1[i];
                }

                byte r = (byte)(x ^ y ^ z);

                result[i] = r;
            }

            MD5 md5 = MD5.Create();

            byte[] bytes = md5.ComputeHash(result);

            Guid guid = new Guid(bytes);

            txtResult.Text = guid.ToString();
        }
    }
}
